package jsp;

import util.CookieUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.*;

/**
 * @author 石小俊
 * @date 2023年07月03日 16:04
 * 该Servlet用于实现用户登录功能
 * 当用户登录的时候判断用户输入的用户名与密码与数据库中的用户名与密码是否一致
 * 如果一致
 *  表示登录成功,将正确的username与password保存到cookie中
 *  且页面跳转到主页main.jsp
 * 如果不一致
 *  则说明输入的用户名或者密码不正确
 *  页面仍然停留在login.jsp页面
 *  且在页面中展示错误消息
 */
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取用户输入的数据
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        // 判断用户输入的用户名与密码是否正确
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        String url = "jdbc:mysql://127.0.0.1:3306/ums?useUnicode=true&characterEncoding=utf-8";
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(url,"root","");
            String sql = new StringBuffer()
                    .append(" select id,username,password ")
                    .append(" from t_user ")
                    .append(" where username = ? ")
                    .append(" and password = ? ")
                    .toString();
            ps = conn.prepareStatement(sql);
            ps.setString(1,username);
            ps.setString(2,password);
            rs = ps.executeQuery();
            if(rs.next()){
                // 登录成功之后将当前用户名与密码保存到cookie中
                CookieUtil.addCookie("username",username,86400,response);
                CookieUtil.addCookie("password",password,86400,response);
                // 页面直接跳转到主页
                request.getRequestDispatcher("/jsp/main.jsp").forward(request,response);
                return;
            }
            // 登录失败转发到登录页面进行登录
            request.setAttribute("msg","用户名或密码错误");
            request.getRequestDispatcher("/jsp/login.jsp").forward(request,response);
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg","服务器内部异常");
            request.getRequestDispatcher("/jsp/login.jsp").forward(request,response);
        } finally {
            if(conn != null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(ps != null){
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(rs != null){
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
